package com.skywolf.chem.live.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.live.entity.Videoitem;
import com.skywolf.chem.live.mapper.VideoitemMapper;
import com.skywolf.chem.live.service.IVideoitemService;
import com.skywolf.chem.manager.param.VideoItemDto;
import com.skywolf.chem.tencent.service.LiveInfoService;
import com.skywolf.chem.utils.SysConfig;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.DescribeMediaInfosRequest;
import com.tencentcloudapi.vod.v20180717.models.DescribeMediaInfosResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 视频附属表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-12-01
 */
@Service
public class VideoitemServiceImpl extends ServiceImpl<VideoitemMapper, Videoitem> implements IVideoitemService {
    Logger logger = LoggerFactory.getLogger(VideoitemServiceImpl.class);

    @Autowired
    VideoitemMapper videoitemMapper;

    @Override
    public List<VideoItemDto> getItemList(Long fatherId) {
        return videoitemMapper.getItemList(fatherId);
    }

    @Override
    public VideoItemDto videoItemInfo(Long id) {
        return videoitemMapper.videoItemInfo(id);
    }

    @Override
    public void delVideoItem(Long id) {
        videoitemMapper.delVideoItem(id);
    }

    @Override
    public int updateVideoItem(VideoItemDto param) {
        Videoitem videoitem = new Videoitem();
        videoitem.setId(param.getId());
        if(param.getChapter()!=null){
            videoitem.setChapter(param.getChapter());
        }
        if(StringUtils.isNotBlank(param.getVideoTitle())){
            videoitem.setVideoTitle(param.getVideoTitle());
        }
        videoitem.setVideoUrl(param.getVideoUrl());
        videoitem.setFileidTx(param.getFileidTx());
        updateVideoMetaData(videoitem.getFileidTx(),videoitem.getId());
        return videoitemMapper.updateById(videoitem);
    }

    @Async
    public void updateVideoMetaData(String fileId,Long videoItemId) {
        String secretId = SysConfig._this.getProperty("tencent_cdn.secret_id");
        String secretKey = SysConfig._this.getProperty("tencent_cdn.secret_key");
        boolean bl = true;
        Integer count = 0;
        Credential cred = new Credential(secretId, secretKey);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("vod.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        VodClient client = new VodClient(cred, "", clientProfile);
        while (bl){
            try{
                count++;
                if(count>2){
                    bl = false;
                }
                DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
                String[] fileIds1 = {fileId};
                req.setFileIds(fileIds1);
                String[] filters1 = {"metaData"};
                req.setFilters(filters1);
                DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);
                Integer duration = resp.getMediaInfoSet()[0].getMetaData().getDuration().intValue();
                Videoitem videoitem = new Videoitem();
                videoitem.setVideoTime(duration+"");
                videoitem.setId(videoItemId);
                this.updateById(videoitem);
                bl = false;
            } catch (TencentCloudSDKException e) {
                logger.error("getGroupUserCount getVideoMetaData:"+videoItemId+" error:{}",e);
            }
        }
    }
}
